package com.ivan.stu.notetool.utils;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.Map;

/**
 * Created by IVAN on 2019/2/21.
 */

public class JsonUtil {
    public static <T> T parseJson(String json, Class<T> classOfT) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        try {
            return gson.fromJson(json, classOfT);
        } catch (Exception e) {
            e.printStackTrace();

            Log.e("解析json时出错！",e.toString());
            return null;
        }

    }

    public static String toJson(Object o) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        try {
            return gson.toJson(o);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> String mapToJson(Map<String, T> map) {
        Gson gson = new Gson();
        try {
            return gson.toJson(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
